﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using UMLDesigner.Widgets;

namespace UMLDesigner.WorkUnits
{
    public class MoveWidgetsWorkUnit : IWorkUnit
    {
        public MoveWidgetsWorkUnit(List<UMLWidget> widgets, Point oldPosition)
        {
            this.widgets = new List<UMLWidget>();
            this.widgets.InsertRange(0, widgets);
            oldPos = oldPosition;
        }

        public Point NewPosition
        {
            set
            {
                newPos = value;
            }
        }

        public void Do(Document doc)
        {
            int dx = newPos.X - oldPos.X;
            int dy = newPos.Y - oldPos.Y;

            MoveWidgets(dx, dy);
        }

        public void Undo(Document doc)
        {
            int dx = oldPos.X - newPos.X;
            int dy = oldPos.Y - newPos.Y;

            MoveWidgets(dx, dy);
        }

        private void MoveWidgets(int dx, int dy)
        {
            foreach (UMLWidget w in widgets)
            {
                w.Location = new Point(w.Location.X + dx, w.Location.Y + dy);
            }
        }


        private List<UMLWidget> widgets;
        private Point oldPos;
        private Point newPos;

    }
}
